package com.cai310.lottery.ticket.protocol.local.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import org.apache.commons.lang.StringUtils;

import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.common.WinningUpdateStatus;
import com.cai310.lottery.entity.lottery.ticket.Ticket;
import com.cai310.lottery.entity.lottery.zc.SczcPasscount;
import com.cai310.lottery.entity.lottery.zc.SczcPeriodData;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.service.ServiceException;
import com.cai310.lottery.support.zc.SczcCompoundItem;
import com.cai310.lottery.support.zc.ZcCompoundPassWork;
import com.cai310.lottery.support.zc.ZcPrizeWork;
import com.cai310.lottery.support.zc.ZcUtils;

public  class SCZCCPUtil extends CPUtil{
	/**
	 *  
	 * @param lottery
	 * @param playType
	 * @return
	 */
	private static final String SINGSPILTCODE = "+";
	private static final String COMPOUNDSPILTCODE = ",";
	@Override
	public String getCpLotteryId(Lottery lottery,Byte betType){
		return "5";
	}
	@Override
	public String getCpBetType(Ticket ticket){
		return "0";
	}
	@Override
	public String getCpPlayType(Ticket ticket){
		if(null==ticket.getMode())return null;
		if(SalesMode.SINGLE.equals(ticket.getMode())){
			return "1";
		}else if(SalesMode.COMPOUND.equals(ticket.getMode())){
			return "0";
		}
		return null;
	}
	@Override
	public String getBetContent(Ticket ticket){
		if(SalesMode.SINGLE.equals(ticket.getMode())){
			return ticket.getContent();
		}else if(SalesMode.COMPOUND.equals(ticket.getMode())){
			return ticket.getContent();
		}
		return null;
	}
	@Override
	public String getSpecialFlag(Ticket ticket) {
		return "";
	}
	@Override
	public String getUpdatePeriodNumber(Ticket ticket) {
		return null;
	}
	public static ZcPrizeWork getZcPrizeWorkByTicket(Ticket ticket, SczcPeriodData periodData) throws DataException{
		if(null==ticket.getMode())return null;
		if(SalesMode.SINGLE.equals(ticket.getMode())){
			return updateSingleResult(ticket,periodData);
		}else if(SalesMode.COMPOUND.equals(ticket.getMode())){
			return updateCompoundResult(ticket,periodData);
		}
		return null;
	}
	public static SczcCompoundItem[] getCompoundContent(Ticket ticket) throws DataException {
		
		if (ticket.getContent() == null) {
			throw new DataException("方案内容获取为空.");
		}
		String[] itemStrs = null;

		itemStrs = ticket.getContent().split(COMPOUNDSPILTCODE);

		if (itemStrs.length != ZcUtils.getMatchCount(Lottery.SCZC) * 2) {
			throw new DataException("方案内容选项个数异常.");
		}
		SczcCompoundItem[] items = new SczcCompoundItem[itemStrs.length];
		for (int i = 0; i < itemStrs.length; i++) {
			items[i] = new SczcCompoundItem(Byte.valueOf(itemStrs[i]).byteValue());
		}
		return items;
	}
	protected static ZcPrizeWork updateCompoundResult(Ticket ticket, SczcPeriodData periodData) throws DataException {
		SczcCompoundItem[] betItems = getCompoundContent(ticket);

		String result = periodData.getResult();// 开奖结果
		if (betItems.length != result.length())
			throw new DataException("投注结果与开奖结果个数不符.");

		ZcCompoundPassWork passWork = new ZcCompoundPassWork(betItems, ticket.getMultiple(), result);
		ZcPrizeWork prizeWork = new ZcPrizeWork(periodData, null, passWork.getLost0_count(), passWork.getLost1_count(),
				passWork.getLost2_count(), passWork.getLost3_count());

		return prizeWork;
	}
	protected static ZcPrizeWork updateSingleResult(Ticket ticket, SczcPeriodData periodData) throws DataException {
		String content = ticket.getContent();
		if(content.indexOf(SINGSPILTCODE)!=-1){
			content = content.replaceAll("\\"+SINGSPILTCODE, "\r\n");
		}
		BufferedReader reader = new BufferedReader(new StringReader(content));
		String result = periodData.getResult();// 开奖结果

		int lost0 = 0;
		int lost1 = 0;
		int lost2 = 0;
		int lost3 = 0;
		int line_count = 0;
		int passcount = 0;
		StringBuffer content0 = new StringBuffer();
		StringBuffer content1 = new StringBuffer();
		StringBuffer content2 = new StringBuffer();
		StringBuffer content3 = new StringBuffer();
		// boolean finalResult = true;

		try {
			String line = reader.readLine();
			int passcountTemp=0;
			while (line != null) {
				if (StringUtils.isNotBlank(line)) {
					if (line.length() != ZcUtils.getMatchCount(Lottery.SCZC)
							&& line.length() != result.length())
						throw new DataException("");
					int lost_count = 0;
					int selected_count = 0;
					char bet, rs_bet;// 投注内容及结果存放变量
					for (int i = 0; i < line.length(); i++) {
						bet = line.charAt(i);
						rs_bet = result.charAt(i);
						if (Character.isDigit(bet)) {
							selected_count++;
						}
						if (Character.isDigit(rs_bet)) {
							if (Character.isDigit(bet) && bet != rs_bet) {
								lost_count++;
							}
						} else {
							// finalResult = false;
						}
					}
					if (lost_count == 0) {
						lost0++;
						content0.append(line).append("\r\n");
					}
					if (lost_count == 1) {
						lost1++;
						content1.append(line).append("\r\n");
					}
					if (lost_count == 2) {
						lost2++;
						content2.append(line).append("\r\n");
					}
					if (lost_count == 3) {
						lost3++;
						content3.append(line).append("\r\n");
					}
					// if (selected_count != this.getSelectedItemPerUnit()) {
					// throw new DataException("");
					// }
					passcountTemp=8-lost_count;
					if(passcountTemp>passcount){
						passcount=passcountTemp;
					}
					line_count++;
				}
				line = reader.readLine();
			}

			if (line_count * ticket.getMultiple() * 2 != ticket.getSchemeCost().intValue())
				throw new DataException("方案内容与投注金额不符.");

		} catch (IOException e) {
			e.printStackTrace();
		}
		int multiple = ticket.getMultiple();
		ZcPrizeWork prizeWork = new ZcPrizeWork(periodData, null, lost0 * multiple, lost1 * multiple, lost2 * multiple,
				lost3 * multiple);
		return prizeWork;
	}
	
	
}
